home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet multimedia / Animacje, filmy i prezentacje / Modelowanie 3D / K-3D 0.6.5.0 / k3d-all-in-one-setup-0.6.5.0.exe / k3d-setup-0.6.5.0.exe / share / shaders / k3d_reflections.h < prev    next >
Encoding:
C/C++ Source or Header  |  2004-05-19  |  7.1 KB  |  196 lines

  1. /************************************************************************
  2.  * reflections.h - Functions which compute reflected light by either
  3.  *                 ray tracing or environment mapping.
  4.  *
  5.  * Author: Larry Gritz (gritzl@acm.org)
  6.  *
  7.  * $Revision: 1.1 $    $Date: 2004/05/19 18:15:20 $
  8.  *
  9.  ************************************************************************/
  10.  
  11.  
  12. #ifndef REFLECTIONS_H
  13. #define REFLECTIONS_H
  14.  
  15. #include "k3d_filterwidth.h"
  16. #include "k3d_raysphere.h"
  17.  
  18.  
  19. /* ReflMap() - Use a reflection map for reflections in flat objects.
  20.  * Inputs are:
  21.  *    reflname - filename of reflection map
  22.  *    P - origin of traced ray
  23.  *    blur - amount of additional blur to add to environment map
  24.  * Outputs are:
  25.  *    return value - the color of incoming light
  26.  *    alpha - opacity of reflection map lookup in the direction R.
  27.  * Warning -  the texture call itself takes derivatives, causing
  28.  * trouble if called inside a loop or varying conditional!  Be cautious.
  29.  */
  30. color ReflMap ( string reflname;  point P; float blur;
  31.                 output float alpha; )
  32. {
  33.     /* Transform to the space of the environment map */
  34.     point Pndc = transform ("NDC", P);
  35.     float x = xcomp(Pndc), y = 1-ycomp(Pndc);
  36.     alpha = float texture (reflname[3], x, y, "blur", blur, "fill", 1);
  37.     return color texture (reflname, x, y, "blur", blur);
  38. }
  39.  
  40.  
  41.  
  42. /* Environment() - A replacement for ordinary environment() lookups, this
  43.  * function ray traces against an environment sphere of known, finite 
  44.  * radius.  Inputs are:
  45.  *    envname - filename of environment map
  46.  *    envspace - name of space environment map was made in
  47.  *    envrad - approximate supposed radius of environment sphere
  48.  *    P, R - position and direction of traced ray
  49.  *    blur - amount of additional blur to add to environment map
  50.  * Outputs are:
  51.  *    return value - the color of incoming environment light
  52.  *    alpha - opacity of environment map lookup in the direction R.
  53.  * Warning -  the environment call itself takes derivatives, causing
  54.  * trouble if called inside a loop or varying conditional!  Be cautious.
  55.  */
  56. color Environment ( string envname, envspace;  uniform float envrad;
  57.                     point P;  vector R;  float blur; output float alpha;)
  58. {
  59.     /* Transform to the space of the environment map */
  60.     point Psp = transform (envspace, P);
  61.     vector Rsp = normalize (vtransform (envspace, R));
  62.     uniform float r2 = envrad * envrad;
  63.     /* Clamp the position to be *inside* the environment sphere */
  64.     if ((vector Psp).(vector Psp) > r2)
  65.         Psp = point (envrad * normalize (vector Psp));
  66.     float t0, t1;
  67.     if (raysphere (Psp, Rsp, envrad, 1.0e-4, t0, t1) > 0)
  68.     Rsp = vector (Psp + t0 * Rsp);
  69.     alpha = float environment (envname[3], Rsp, "blur", blur, "fill", 1);
  70.     return color environment (envname, Rsp, "blur", blur);
  71. }
  72.  
  73.  
  74.  
  75.  
  76. /* RayTrace() - A fancy ray trace routine, particularly suitable for
  77.  * use with the "ray server."  Tries to sample over the surface
  78.  * element and over the varying ray spread due to surface curvature.
  79.  * An ordinary call to trace would point sample the environment in a
  80.  * very simplistic way.  This function takes the size of the surface
  81.  * facet and curvature of the surface into account, and lets you
  82.  * sample the space with multiple rays.
  83.  * 
  84.  * Inputs:
  85.  *    P - surface position
  86.  *    Rdir - the unit-length reflection direction.
  87.  *    blur - reflection blurriness; 0 = sharp reflection
  88.  *    jitter - when 1, fully jitter the stochastic ray directions.  Lower
  89.  *          numbers jitter less, 0 doesn't jitter.  Lowering jitter may help
  90.  *          alleviate "sparkling" due to animation with low nrays.
  91.  *    nsamples - number of rays with which to sample.  Larger numbers will
  92.  *          yield better-sampled reflections, but will be more expensive.
  93.  *          Note that the function reduces this number for secondary rays,
  94.  *          assuming that the distribution from primary rays will be
  95.  *          sufficient!
  96.  * Return value: the average of the trace calls.
  97.  * 
  98.  * Warning!!! This function takes derivatives to find out the ray spread!
  99.  * This can cause trouble if RayTrace() is called inside a loop or varying
  100.  * conditional!  Be cautious.
  101.  */
  102. color
  103. RayTrace (point P;  vector Rdir;  float Kr, blur, jitter;  
  104.           uniform float nsamples;  output float alpha;)
  105. {
  106. #if (defined(BMRT) || defined(RAYSERVER_H))
  107.     float rand () {
  108.         extern float jitter;
  109.         return (raylevel()==0) ? (0.5 + jitter * (float random() - 0.5)) : 0.5;
  110.     }
  111.     extern float du, dv;
  112.     color C, Ct;
  113.     float hitdist; point Phit, Pmiss;  vector Nhit, Rmiss;
  114.     float bluramt = blur + filterwidthp(Rdir);
  115.     uniform float nrays = (raylevel() == 0 ? max(1,ceil(sqrt(nsamples))) : 1);
  116.     vector Tu = Du(P) * (1.5 * du); /* overblur just a tad... */
  117.     vector Tv = Dv(P) * (1.5 * dv);
  118.     if (Kr < 0.0001) {
  119.     C = 0;
  120.     } else if (bluramt > 0 || nrays > 1) {
  121.         /* Construct orthogonal components to Rdir */
  122.         vector uoffset = blur * normalize (vector (zcomp(Rdir) - ycomp(Rdir),
  123.                                                    xcomp(Rdir) - zcomp(Rdir),
  124.                                                    ycomp(Rdir) - xcomp(Rdir)));
  125.         vector voffset = Rdir ^ uoffset;
  126.         uniform float i, j;
  127.         C = 0;  alpha = 0;
  128.         for (i = 0;  i < nrays;  i += 1) {
  129.             for (j = 0;  j < nrays;  j += 1) {
  130.                 /* Add a random offset to the smooth reflection vector */
  131.                 vector R = Rdir + ((i + rand())/nrays - 0.5) * uoffset +
  132.                                   ((j + rand())/nrays - 0.5) * voffset;
  133.         R = normalize(R);
  134.                 point Pray = P +  ((j + rand())/nrays - 0.5) * Tu +
  135.                                   ((i + rand())/nrays - 0.5) * Tv;
  136. #pragma nolint 3  /* this call intentionally passes uninitialized vars */
  137.         fulltrace (Pray, R, Ct, hitdist, Phit, Nhit, Pmiss, Rmiss);
  138.         C += Ct;
  139.         alpha += 1 - step(1.0e10,hitdist);
  140.             }
  141.         }
  142.     uniform float totrays = nrays*nrays;
  143.         C /= totrays;   alpha /= totrays;
  144.     } else {
  145.         /* No blur or curvature, just do a simple trace */
  146. #pragma nolint 2  /* this call intentionally passes uninitialized vars */
  147.     fulltrace (P, Rdir, C, hitdist, Phit, Nhit, Pmiss, Rmiss);
  148.     alpha = 1 - step(1.0e10,hitdist);
  149.     }
  150.     return C;
  151. #else
  152.     return color 0;
  153. #endif
  154. }
  155.  
  156.  
  157.  
  158.  
  159.  
  160. #define ENVPARAMS \
  161.         envname, envspace, envrad, rayjitter, raysamples
  162.  
  163. #define DECLARE_ENVPARAMS                           \
  164.         string envname, envspace;                   \
  165.         uniform float envrad, rayjitter, raysamples
  166.  
  167. #define DECLARE_DEFAULTED_ENVPARAMS                                 \
  168.         string envname = "", envspace = "world";                    \
  169.         uniform float envrad = 100, rayjitter = 0, raysamples = 1
  170.  
  171.  
  172.  
  173. color
  174. SampleEnvironment (point P;  vector R;  float Kr, blur;  DECLARE_ENVPARAMS;)
  175. {
  176.     color C = 0;
  177.     float alpha;
  178.     if (envname != "") {
  179.     if (envspace == "NDC")
  180.         C = ReflMap (envname, P, blur, alpha);
  181.     else
  182.         C = Environment (envname, envspace, envrad, P, R, blur, alpha);
  183.     }
  184. #if (defined(BMRT) || defined(RAYSERVER_H))
  185.     color Cray = RayTrace (P, R, Kr, sqrt(blur), rayjitter, raysamples, alpha);
  186.     C = Cray + (1-alpha) * C;
  187. #endif
  188.     return Kr * C;
  189. }
  190.  
  191.  
  192.  
  193.  
  194. #endif /* defined(REFLECTIONS_H) */
  195.  
  196.